/**   
 * @Title: RechargeMenuPage.java 
 * @Package com.esun.elk.ui.views.userinfo.recharge 
 * @Description: 充值功能选项界面
 * @author Miko baoz@500wan.com   
 * @date 2011-5-25 上午11:50:40  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.recharg;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.model.UserInfo;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.rabbit2.communicaters.recharg.LoginControllerRecharge;
import com.esun.rabbit2.communicaters.recharg.RechargeController;
import com.esun.rabbit2.communicaters.recharg.RechargeUserInfo;
import com.esun.rabbit2.communicaters.recharg.UserInfoDetailController;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

/**
 * @ClassName: RechargeMenuPage
 * @Description: 充值功能选项界面
 * @author Miko baoz@500wan.com
 * @date 2011-5-25 上午11:50:40
 * 
 */
public class RechargeMenuPage extends Activity implements OnClickListener {

	private View btnUnin;
	private View btnShenzhouxing;
	private RelativeLayout btnZhiFuBao;
	private ImageButton btnGoBack;
	
	// kuroky added 2012-02-07
	private RelativeLayout btnCft;
	//kael gao added 2012-05-29
	private RelativeLayout btnYl;

	private Handler handler;

	private LoginControllerRecharge loginRecharge = new LoginControllerRecharge();
	private UserInfoDetailController detailInfoController = new UserInfoDetailController();
	private RechargeController rechargeController = new RechargeController();

	private ProgressDialog progress;

	private final int SHENZHOUXING_PREPARE_OK = 10000;
	private final int UNIN_PREPARE_OK = 10001;
	private final int NOT_BINDING_UNIN_CARD = 10002;
	private final int CAN_NOT_PAY_BY_PHONE = 22222;

	private final int PREPARE_FAILED = 111111;

	public static boolean isRechargeSucceed = false;
	

	// Activity 请求代码
	public static final int REQUEST_CODE_BANKCAD = 1;
	// Activity返回代码
	public static final int RESULT_CODE_TO_CENTER = 10;
	public static final int RESULT_CODE_TO_BUY_LOTTERY = 11;

	// 绑定银行卡类型

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.userinfo_detail_recharge);

		initUI();
		initHandler();
	}

	private void initUI() {
		btnUnin = findViewById(R.id.userinfo_detail_recharge_btn_unin);
		btnUnin.setOnClickListener(this);

		btnShenzhouxing = findViewById(R.id.userinfo_detail_recharge_btn_szx);
		btnShenzhouxing.setOnClickListener(this);

		btnZhiFuBao = (RelativeLayout) findViewById(R.id.userinfo_detail_recharge_btn_zfb);
		btnZhiFuBao.setOnClickListener(this);
		
		// kuroky added 2012-02-07
		btnCft = (RelativeLayout) findViewById(R.id.userinfo_detail_recharge_btn_cft);
		btnCft.setOnClickListener(this);
		// kael gao added 2012-05-29
		btnYl = (RelativeLayout) findViewById(R.id.userinfo_detail_recharge_btn_yl);
		btnYl.setOnClickListener(this);

		btnGoBack = (ImageButton) findViewById(R.id.userinfo_detail_recharge_goBack);
		btnGoBack.setOnClickListener(this);

		progress = ViewerUtil.showProgressDialog(this, "用户信息获取中...");
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				progress.cancel();

				switch (msg.what) {

				case SHENZHOUXING_PREPARE_OK:

					startActivity(new Intent().setClass(RechargeMenuPage.this, RechargeShenzhouxin.class));

					break;

				case UNIN_PREPARE_OK:

//					startActivity(new Intent().setClass(RechargeMenuPage.this, RechargeUninBinding.class));
					startActivityForResult(new Intent(RechargeMenuPage.this, RechargeYinLianWebView.class).putExtra(Key.KEY_URL,Config.ACTIVITY_RECHARGE_YINHANGKA_BIND_URL).putExtra("TAG", Config.NUM1), REQUEST_CODE_BANKCAD);
					break;

				case NOT_BINDING_UNIN_CARD:

//					startActivity(new Intent().setClass(RechargeMenuPage.this, RechargeUninUnbinding.class));
					startActivityForResult(new Intent(RechargeMenuPage.this, RechargeYinLianWebView.class).putExtra(Key.KEY_URL, Config.ACTIVITY_RECHARGE_YINHANGKA_UNBIND_URL).putExtra("TAG", Config.NUM0), REQUEST_CODE_BANKCAD);
					break;

				case CAN_NOT_PAY_BY_PHONE:

					ViewerUtil.showTipDialog(RechargeMenuPage.this, rechargeController.getMSG(), false);
//					startActivityForResult(new Intent(RechargeMenuPage.this, RechargeYinLianWebView.class), REQUEST_CODE_BANKCAD);
//					startActivityForResult(new Intent(RechargeMenuPage.this, RechargeYinLianWebView.class).putExtra(Key.KEY_URL,Config.ACTIVITY_RECHARGE_YINHANGKA_BIND_URL).putExtra("TAG", Config.NUM1), REQUEST_CODE_BANKCAD);
//					startActivityForResult(new Intent(RechargeMenuPage.this, RechargeYinLianWebView.class).putExtra(Key.KEY_URL, Config.ACTIVITY_RECHARGE_YINHANGKA_UNBIND_URL).putExtra("TAG", Config.NUM0), REQUEST_CODE_BANKCAD);
					break;

				case PREPARE_FAILED:

					//ViewerUtil.showTipDialog(RechargeMenuPage.this, "获取用户信息失败", false);
				  Toast.makeText(RechargeMenuPage.this, Language.STR_CONNECTIONFAIL.toString(), Toast.LENGTH_SHORT).show();
					break;
				}

			}
		};
	}

	private boolean checkUserLogin() {

		if (RechargeUserInfo.getUserInfo().getSessionId().length() > 5) {
			return true;
		} else {
			boolean b = false;

			try {

				b = loginRecharge.login(UserInfo.getUserInfo().getUserName(), UserInfo.getUserInfo().getUserPwd());
			} catch (Exception e) {
				EDebug.o(this, "Error" + e.getLocalizedMessage());
				b = false;
			}
			return b;
		}
	}

	private void prepareUnin() {
		new Thread() {
			@Override
			public void run() {
				super.run();

				// 是否登陆成功
				if (checkUserLogin()) {

					// 是否获取用户详情成功
					if (detailInfoController.getUserInfoDetail(UserInfo.getUserInfo().getUserName(), RechargeUserInfo.getUserInfo().getSessionId()).length > 1) {

						// 是否绑定银行卡
						if (RechargeUserInfo.getUserInfo().isBandingCard()) {

							// 是否可以使用手机银联充值
							if (rechargeController.isCanPhoneRecharge()) {
								Message msg = handler.obtainMessage(UNIN_PREPARE_OK);
								handler.sendMessage(msg);
							} else {
								Message msg = handler.obtainMessage(CAN_NOT_PAY_BY_PHONE);
								handler.sendMessage(msg);
							}

						} else {
							Message msg = handler.obtainMessage(NOT_BINDING_UNIN_CARD);
							handler.sendMessage(msg);
						}
					} else {
						Message msg = handler.obtainMessage(PREPARE_FAILED);
						handler.sendMessage(msg);
					}

				} else {
					Message msg = handler.obtainMessage(PREPARE_FAILED);
					handler.sendMessage(msg);
				}
			}
		}.start();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.userinfo_detail_recharge_btn_unin:

			progress.show();
			prepareUnin();

			break;

		case R.id.userinfo_detail_recharge_btn_szx:

			startActivity(new Intent().setClass(RechargeMenuPage.this, RechargeShenzhouxin.class));

			break;

		case R.id.userinfo_detail_recharge_goBack:
			UserInfo.getUserInfo().setBuyingRecharge(false);

			finish();

			break;

		case R.id.userinfo_detail_recharge_btn_zfb:

			startActivity(new Intent().setClass(RechargeMenuPage.this, RechargeZhiFuBao.class));

			break;
			
		case R.id.userinfo_detail_recharge_btn_cft:

			startActivity(new Intent().setClass(RechargeMenuPage.this, RechargeCaifutong.class));

			break;
			
		case R.id.userinfo_detail_recharge_btn_yl:
			startActivity(new Intent().setClass(RechargeMenuPage.this, RechargeYinLian.class));
			break;
		default:
			break;
		}

	}

	@Override
	protected void onPause() {
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
		if (isRechargeSucceed) {
			RechargeMenuPage.this.finish();
			isRechargeSucceed = false;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		if(keyCode == KeyEvent.KEYCODE_BACK){
			UserInfo.getUserInfo().setBuyingRecharge(false);
			finish();
		}
		
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
		EDebug.o(RechargeMenuPage.this, "shit！！回来了，onActivityResult被调用");
		switch (requestCode) {
		case REQUEST_CODE_BANKCAD:
			if (resultCode == RESULT_CODE_TO_CENTER) {
				EDebug.o(RechargeMenuPage.this, "查看账户");
				setResult(RESULT_CODE_TO_CENTER);
				RechargeMenuPage.this.finish();
				
				
			}else if (resultCode == RESULT_CODE_TO_BUY_LOTTERY){
				EDebug.o(RechargeMenuPage.this, "立即购彩");
				setResult(RESULT_CODE_TO_BUY_LOTTERY);
				
				finish();
			}
			break;

		default:
			break;
		}
	}
	
	
	
}
